class taskItem extends eui.Component{

	private lb_desc:eui.Label;
	private lb_award:eui.Label;
	private btn_get:BaseButton;
	private head_icon:eui.Image;
	private head_frame:eui.Image;
	private btn_add:BaseButton;
	private itemIndex:number;
	private state:string;
	private icon:string;
	private money:number;

	public constructor(prama?) {
		super();
		this.state = prama.state;
		this.itemIndex = prama.index;
		this.icon = prama.icon;
		this.money = prama.money;
		this.skinName = "TaskItem";
	}

	protected childrenCreated():void{
		super.childrenCreated();
		this.updateUI();
		this.btn_get.addTapHandler(this.btnHandle, this);
		this.btn_add.addTapHandler(this.inviteFriend, this)
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.destory, this);
	}

	protected btnHandle()
	{
		if(this.btn_get.currentState == 'invite')
		{
			//邀请	
			MessageUtil.tips('还未完成任务哦~');
		}
		else if(this.btn_get.currentState == 'received')
		{
			//已领取
			MessageUtil.tips('已经领取过奖励了哦~');
		}
		else if(this.btn_get.currentState == 'get')
		{
			this.receiveMoney();
		}
	}

	public updateUI()
	{
		this.lb_desc.text = '邀请第'+(this.itemIndex+1)+'位好友';
		this.lb_award.text = '$' + this.money;

		if(this.state=="0")
		{
			this.btn_add.visible = true;
			this.head_icon.visible = false;
			this.head_frame.visible = false;
			this.btn_get.currentState = 'invite';
		}
		else
		{
			this.btn_add.visible = false;
			this.head_icon.visible = true;
			this.head_frame.visible = true;
			if(this.icon == "" || this.icon == "1")
			{
				this.head_icon.source = 'normal_icon_jpg';
			}
			else
			{
				this.head_icon.source = this.icon;
			}
			if(this.state == "2")
			{
				this.btn_get.currentState = 'received';
			}
			else
			{
				this.btn_get.currentState = 'get';
			}
		}
		
	}
	
    // 邀请好友
    public inviteFriend(){
        let openid = RoleCache.instance().openid
        // platform.shareAppMessage(openid , position)
        let data = {
			position:this.itemIndex,
            type:ShareId.default
        }
		
        SharePrase.instance().requestOneAdvertisement(data);
    }

	protected destory()
	{
		this.btn_get.removeTapHandler();
		this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.destory, this);
		this.btn_add.removeTapHandler();
		this.state = null;
		this.itemIndex = null;
		this.icon = null;
	}

	// 领取金钱
    public receiveMoney(){
        let data = {
            openid : RoleCache.instance().openid,
            position : this.itemIndex
        }

        HttpManager.instance().sendPost(GameConst.httpUrl + 'options/get_task_money' , StringUtil.jsonToHttpPostString(data) , (e)=>{
            let data = JSON.parse(e);

            RoleCache.instance().data.money = data.data.money;
            GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGED);
            MessageUtil.tips("领取成功");
			this.state = "2";
			this.btn_get.currentState = 'received';

			// let logic:TaskListUI = UIManager.instance().getLogicByName(UIConst.TASK_UI) as TaskListUI;
			// if(logic)
			// {
			// 	logic.updateItem();
			// }
        })
    }
}

window['taskItem'] = taskItem;